package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
@Api(tags = "检查项相关接口")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param item
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result addCheckItem(@RequestBody CheckItem item){
        log.info("新增检查项:{}",item);
        checkItemService.addCheckItem(item);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS,null);
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @ApiOperation("根据id删除检查项")
    @GetMapping("/delete")
    public Result deleteCheckItem(Integer id){
        log.info("根据id删除检查项:{}",id);
        checkItemService.deleteCheckItem(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,null);
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param checkgroupId
     * @return
     */
    @ApiOperation("根据检查组id查询关联的检查项id")
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        log.info("根据检查组id查询关联的检查项id:{}",checkgroupId);
        List<Integer> list = checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    /**
     * 编辑检查项
     * @param item
     * @return
     */
    @ApiOperation("编辑检查项")
    @PostMapping("/edit")
    public Result editCheckItem(@RequestBody CheckItem item){
        log.info("编辑检查项:{}",item);
        checkItemService.editCheckItem(item);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,null);
    }

    /**
     * 分页查询检查项
     * @param pageBean
     * @return
     */
    @PostMapping("findPage")
    @ApiOperation("分页查询检查项")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        log.info("分页查询检查项:{}",pageBean);
        PageResult result = checkItemService.findPage(pageBean);
        return result;
    }

    /**
     * 查询所有检查项
     * @return
     */
    @ApiOperation("查询所有检查项")
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> list = checkItemService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @ApiOperation("根据id查询检查项")
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据查询检查项:{}",id);
        CheckItem result = checkItemService.findById(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,result);
    }
}
